package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test5 {
    public static void main(String[] args) throws Exception {

        File dir = new File(
                Test5.class.getResource(".").toURI()
        );
        String packageName = Test5.class.getPackage().getName();

        File[] files = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File file : files) {
            String className = file.getName().substring(0, file.getName().indexOf("."));

            Class<?> cls = Class.forName(packageName + "." + className);
            boolean b = cls.isAnnotationPresent(AutoRunClass.class);


            if (b){
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods) {
                    boolean b1 = method.isAnnotationPresent(AutoRunMethod.class);
                    if (b1){
                        method.invoke(cls.newInstance());
                    }
                }
            }
        }


    }
}
